package com.itheima.text;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试用例
 */
public class MyBatis {
   @Test
   public void testSelectAll() throws IOException {
      //获取sqlSessionFactory对象
      String resource = "mybatis-config.xml";
      InputStream inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      //获取sqlSession对象,执行sql
      SqlSession sqlSession = sqlSessionFactory.openSession();
      //获取BrandMapper接口的代理对象
      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      //执行BrandMapper接口代理的方法
      List<Brand> brands = mapper.BrandSelectAll();
      System.out.println(brands);
      //释放资源
      sqlSession.close();
   }


   @Test
   public void testSelectByID() throws IOException {
      //获取sqlSessionFactory对象
      String resource = "mybatis-config.xml";
      InputStream inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      //获取sqlSession对象,执行sql
      SqlSession sqlSession = sqlSessionFactory.openSession();
      //获取BrandMapper接口的代理对象
      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      //执行BrandMapper接口代理的方法
      Brand brand = mapper.selectBrand(4);
      System.out.println(brand);
      //释放资源
      sqlSession.close();
   }


   //多添加查询
   @Test
   public void testSelectByCondition() throws IOException {
      //1.定义临时接收静态变量
      int status = 1;
      String companyName = "小米";
      String brandName = "小米";
      //处理参数(因为是like模糊查询)
      companyName = "%" + companyName + "%";
      brandName = "%" + brandName + "%";

      //2.创建一个对象接收参数
      Brand brand = new Brand();
      brand.setStatus(status);
      brand.setCompanyName(companyName);
      brand.setBrandName(brandName);


      //3.使用Map集合
      Map map = new HashMap<>();
      map.put("status",status);
      //map.put("companyName",companyName);
      map.put("brandName", brandName);
      //获取sqlSessionFactory对象
      String resource = "mybatis-config.xml";
      InputStream inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      //获取sqlSession对象,执行sql
      SqlSession sqlSession = sqlSessionFactory.openSession();
      //获取BrandMapper接口的代理对象
      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      //执行BrandMapper接口代理的方法
      List<Brand> brands = mapper.selectByCondition(map);
      System.out.println(brands);
      //释放资源
      sqlSession.close();
   }

   //单条件动态查询
   @Test
   public void testSelectByConditionSingle() throws IOException {
      //1.定义临时接收静态变量
      int status = 1;
      String companyName = "小米";
      String brandName = "小米";
      //处理参数(因为是like模糊查询)
      companyName = "%" + companyName + "%";
      brandName = "%" + brandName + "%";

      //2.创建一个对象接收参数
      Brand brand = new Brand();
      //brand.setStatus(status);
      brand.setCompanyName(companyName);
      //brand.setBrandName(brandName);

      //获取sqlSessionFactory对象
      String resource = "mybatis-config.xml";
      InputStream inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      //获取sqlSession对象,执行sql
      SqlSession sqlSession = sqlSessionFactory.openSession();
      //获取BrandMapper接口的代理对象
      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      //执行BrandMapper接口代理的方法
      List<Brand> brands = mapper.selectByConditionSingle(brand);
      System.out.println(brands);
      //释放资源
      sqlSession.close();
   }

   //添加
   @Test
   public void addAll() throws IOException {
      //1.定义临时接收静态变量
      String companyName = "华硕";
      String brandName = "拯救者7000";
      int ordered = 100;
      String description = "让电脑走进家家户户";
      int status = 1;
      //2.创建一个对象接收参数
      Brand brand = new Brand();
      brand.setStatus(status);
      brand.setCompanyName(companyName);
      brand.setBrandName(brandName);
      brand.setOrdered(ordered);
      brand.setDescription(description);
      //获取sqlSessionFactory对象
      String resource = "mybatis-config.xml";
      InputStream inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      //获取sqlSession对象,执行sql
      SqlSession sqlSession = sqlSessionFactory.openSession(true);
      //获取BrandMapper接口的代理对象
      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      //执行BrandMapper接口代理的方法
      mapper.addAll(brand);
      //多一步提交事务
      //sqlSession.commit();
      //释放资源
      sqlSession.close();
   }


   //修改
   @Test
   public void updateAll() throws IOException {
      //1.定义临时接收静态变量
      String companyName = "玻璃";
      String brandName = "广东玻璃";
      int ordered = 200;
      String description = "你的心碎了我的不会碎";
      int status = 1;

      int id=9;
      //2.创建一个对象接收参数
      Brand brand = new Brand();
      brand.setStatus(status);
      brand.setCompanyName(companyName);
      brand.setBrandName(brandName);
      brand.setOrdered(ordered);
      brand.setDescription(description);
      brand.setId(id);
      //获取sqlSessionFactory对象
      String resource = "mybatis-config.xml";
      InputStream inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      //获取sqlSession对象,执行sql
      SqlSession sqlSession = sqlSessionFactory.openSession(true);
      //获取BrandMapper接口的代理对象
      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      //执行BrandMapper接口代理的方法
      mapper.update(brand);
      //释放资源
      sqlSession.close();
   }



   //动态修改
   @Test
   public void updateAllStringLg() throws IOException {
      //1.定义临时接收静态变量
      String companyName = "玻璃";
      String brandName = "拯救者";
      int ordered = 200;
      String description = "你的心碎了我的不会碎";
      int status = 1;

      int id=10;
      //2.创建一个对象接收参数
      Brand brand = new Brand();
     // brand.setStatus(status);
     // brand.setCompanyName(companyName);
      brand.setBrandName(brandName);
      //brand.setOrdered(ordered);
     // brand.setDescription(description);
      brand.setId(id);
      //获取sqlSessionFactory对象
      String resource = "mybatis-config.xml";
      InputStream inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      //获取sqlSession对象,执行sql
      SqlSession sqlSession = sqlSessionFactory.openSession(true);
      //获取BrandMapper接口的代理对象
      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      //执行BrandMapper接口代理的方法
      int i = mapper.updateSingle(brand);
      System.out.println("影响了第:"+i+"行");
      //释放资源
      sqlSession.close();
   }


   //动态批量删除
   @Test
   public void deleteBYids() throws IOException {
      //1.定义临时接收静态变量
      int[] id={9,10};
      //获取sqlSessionFactory对象
      String resource = "mybatis-config.xml";
      InputStream inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      //获取sqlSession对象,执行sql
      SqlSession sqlSession = sqlSessionFactory.openSession(true);
      //获取BrandMapper接口的代理对象
      BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      //执行BrandMapper接口代理的方法
      mapper.deleteByIdS(id);
      //释放资源
      sqlSession.close();
   }
}